<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/25
 * Time: 19:15
 */
namespace app\appmenu\controller;

use app\common\model\Appmenu;
use think\facade\View;
use think\facade\Request;
use app\admin\controller\AdminBase;

class Admin extends AdminBase
{
    public function index()
    {
        if (!Appmenu::check_menu_token()) {
            return redirect(url('/appmenu/bind_menu'));
        }

        // 取菜单信息
        $menu_list = Appmenu::get_menu_list(0, $this->open_card_id);

        return View::fetch('index',[
            'menu_list'      => $menu_list,
            'menu_lenth_arr' => array(0, 1, 2),
            'store_id'       => $this->store_id,
        ]);
    }

    public function save()
    {
        if (!$this->request->isPost()) {
            return ['info'=>'系统错误'];
        }

        $id        = (int)Request::post('id', 0);
        $title     = trim(Request::post('title'));
        $parent_id = (int)Request::post('parent_id', 0);
        $store_id  = (int)Request::param('store_id', 0);
        $content   = '';

        if (!$store_id) {
            $store_id = $this->open_card_id;
        }

        $info = array(
            'title'          => $title,
            'store_id'       => $store_id,
            'content'        => $content,
        );

        if (!$id) {
            $menu_num = Appmenu::get_menu_num($parent_id, $store_id);
            if ($parent_id) {
                if ($menu_num >= 5) {
                    return ['info' => '子级菜单最多只能加5个'];
                }

                $parent_info = _uri('appmenu', ['id' => $parent_id]);
                if (!$parent_info) {
                    return ['info' => '父级菜单不存在'];
                }

                $info['parent_id'] = $parent_id;
            } else {
                if ($menu_num >= 3) {
                    return ['info' => '父级菜单最多只能加3个'];
                }
            }

            $menu_num = Appmenu::get_menu_num($parent_id, $store_id);
            $info['view_order'] = $menu_num;
        }

        if (!$title) {
            return ['info' => '菜单标题不能为空'];
        }

        $info['open_card_id'] = $this->open_card_id;

        if ($id) {
            $menu_info = _uri('appmenu', ['id' => $id]);
            if (!$menu_info) {
                return ['info' => '该菜单不存在'];
            }

            Appmenu::update($info, ['id'=>$id]);
        } else {

            $appmenu = Appmenu::create($info);
            $id = $appmenu->id;
        }

        return array('info'=>'ok', 'id'=>$id, 'title'=>$title, 'parent_id'=>$parent_id);
    }

    /**
     * 保存菜单内容
     */
    public function save_content()
    {
        $id      = Request::post('id', 0);
        $content = trim(Request::post('content'));
        $type    = Request::post('type', 1);

        if (!$id) {
            return ['info' => '该菜单不存在'];
        }
        $menu_info = _uri('appmenu', ['id'=>$id]);
        if (!$menu_info) {
            return ['info' => '该菜单不存在'];
        }

        // 如果是文本回复，因为用到到富文本编辑器，所以特殊处理
        if ($type == 20) {
            $content = customer_content_filter($content);
        } else {
            $content = trim($content);
        }

        if (!$content) {
            return ['info' =>'菜单内容不能为空'];
        }
        if ($type == 10) {
            $content = check_http_start($content);

        } else if ($type == 60) {
            $devid_str = config('appmenu.miniprogram_devide');
            $mini_data = explode($devid_str, $content);
            if (!isset($mini_data[0]) || !$mini_data[0]) {
                return ['info' =>'请输入小程序appid'];
            }
            if (!isset($mini_data[1]) || !$mini_data[1]) {
                return ['info' => '请输入小程序路径'];
            }
            if (!isset($mini_data[2]) || !$mini_data[2]) {
                return ['info' => '请输入小程序备用页面地址'];
            }
        }
        /* // 链接
        if ($type == 1) {

            // 纯文本
        } else if ($type == 20) {

        } else if ($type == 30 || $type == 40) {

        } */

        Appmenu::update(['type'=>$type, 'content'=>$content], ['id'=>$id]);

        return ['info' => 'ok'];
    }


    /**
     * 显示已调整的元素
     */
    public function show_content()
    {
        $id          = Request::post('id', 0);

        $menu_info = _uri('appmenu', ['id'=>$id]);
        if (!$menu_info) {
            return ['info' => '该菜单不存在'];
        }

        if ($menu_info['type'] == 1) {

        } else {
            $html = '<div style="margin-top:20px;" data-content-type="'.$menu_info['type'].'">'.$menu_info['content'].'</div>';
        }

        echo $html;
    }

    /**
     * 删除菜单
     */
    public function delete()
    {
        $id = Request::post('id', 0);
        if (!$id) {
            return ['info' => '请选择要删除的菜单'];
        }

        $menu_info = _uri('appmenu', ['id' => $id]);
        if (!$menu_info) {
            return ['info' => '该菜单不存在'];
        }

        Appmenu::where(['id' => $id])->delete();
        Appmenu::where(['parent_id' => $id])->delete();

        return 'ok';
    }

    /**
     * 通过菜单内容取得菜单的图文列表
     */
    public function get_menu_msg_list()
    {
        $id          = Request::post('id', 0);
        $type        = Request::post('type', 1);    // 图文类型：1单图 2多图

        $menu_info = _uri('appmenu', ['id' => $id]);
        if (!$menu_info) {
            return '该菜单不存在';
        }

        $filter = array(
            'type' => $type
        );
        if ($menu_info && in_array($menu_info['type'], array(3, 4))) {
            $filter['select_id'] = $menu_info['content'];
        }

        $filter['open_card_id'] = $this->open_card_id;
        $filter['store_id']     = $this->open_card_id;

//        //$list = _widget('appmsg')->get_list($filter);
        $list = getList('appmsg', $filter);
        View::assign('list', $list);

        $template_path = MODULE_PATH."/appmsg/view/lib/";
        if ($type == 1) {
            $template_path .= 'select_single_list.html';
        } else {
            $template_path .= 'select_multi_list.html';
        }
        $html = View::fetch($template_path);

        echo $html;
    }

    public function sort_menu()
    {
        $store_id = Request::param('store_id', $this->store_id);
        if (!$store_id) {
            $store_id = $this->open_card_id;
        }

        // 取菜单信息
        $menu_list = Appmenu::get_menu_list(0, $store_id);

        return View::fetch('sort',[
            'menu_list'      => $menu_list,
            'menu_lenth_arr' =>  array(0, 1, 2)
        ]);
    }


    public function save_sort()
    {
        $store_id =  Request::post('store_id', 0);
        if (!$store_id) {
            $store_id = $this->open_card_id;
        }

        $data_parent = Request::param('data_parent', array());
        $data_child  = Request::param('data_child', array());

        if ($data_parent) {
            foreach ($data_parent as $k=>$v) {
                Appmenu::update(array('view_order'=>$v), ['id'=>$k]);
            }
        }
        if ($data_child) {
            foreach ($data_child as $k=>$v) {
                Appmenu::update(array('view_order'=>$v), ['id'=>$k]);
            }
        }

        return ['info'=>'ok'];
    }

    public function load_menu_preview()
    {
        $store_id = Request::request('store_id', 0);
        if (!$store_id) {
            $store_id = $this->open_card_id;
        }

        $menu_list = Appmenu::get_menu_list_for_weixin(0, $store_id);

        echo  View::fetch('menu_preview', [
            'menu_list' => $menu_list
        ]);
    }
}